package com.bicon.botu.mall.base.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.base.common.MallHomePageConstants;
import com.bicon.botu.mall.base.service.IChannelService;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.base.entity.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 频道 前端控制器
 *
 * @author 张凡
 * @date 2017/7/19 下午 14:49
 */
@RestController
@CrossOrigin
@RequestMapping("/V1.0.0/homePage/channelManagement")
public class ChannelController extends BaseController {
    @Autowired
    IChannelService iChannelService;

    /**
     * 增加频道
     *
     * @param channelTitle 频道名称
     * @param channelFlag  频道开关
     * @param channelType  频道类型： 1-猜你喜欢 2-新品首发 3-人气推荐
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam("channelTitle") String channelTitle,
                             @RequestParam("channelFlag") Integer channelFlag,
                             @RequestParam("channelType") Integer channelType) {
        return iChannelService.insert(channelTitle, channelFlag, channelType);
    }

    /**
     * 修改频道
     *
     * @param channelId    频道id
     * @param channelTitle 频道名称
     * @param channelFlag  频道开关
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam("channelId") Integer channelId,
                             @RequestParam("channelTitle") String channelTitle,
                             @RequestParam(value = "channelFlag", required = false) Integer channelFlag) {
        return iChannelService.update(channelId, channelTitle, channelFlag);
    }

    /**
     * 逻辑删除频道
     *
     * @param id 频道Id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult deleteChannel(@RequestParam("id") Integer id) {
        return iChannelService.deleteChannel(id);
    }

    /**
     * 获取频道列表
     * @return
     */
    @RequestMapping(value = "/selectChannel", method = RequestMethod.GET)
    public BaseResult selectChannel() {
        List<Channel> channels = new Channel().selectList(new EntityWrapper<Channel>()
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL));
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200, channels);
    }
}
